import * as echarts from '../../../ec-canvas/echarts';
import Dialog from '@vant/weapp/dialog/dialog';
var option = {
  title: {
    // 标题
    text: '城市浪费情况',
    left: '1%',
    top: 5,
    textStyle: {
      fontSize:13
    }
  },
  // grid:{
  //   top:10,
  //   bottom:30,
  //   left:30,
  //   right:10
  // },
  tooltip: {
    trigger: 'item',
    position: function (pos) {
      // 鼠标在左侧时 tooltip 显示到右侧，鼠标在右侧时 tooltip 显示到左侧。
      let x;
      let y = pos[1];
      if (pos[0] <= 80) {
        x = pos[0]
      } else {
        x = pos[0] - 80
      }
      return [x, y];
    },
  },
  series : [
    {
        // name: '访问来源',
        type: 'pie',
        radius: '70%',
        data:[
            {value:235, name:'北京'},
            {value:274, name:'天津'},
            {value:310, name:'上海'},
            {value:335, name:'广东'},
            {value:400, name:'南京'}
        ].sort(function (a, b) { return a.value - b.value; }),
        label: {
          show: false,
        },
        // label: {
        //   fontSize: 10
        // },
        // labelLine: {
        //   show: true,
        //   length: 0
        // }
    }
]
};
function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  chart.setOption(option);
  return chart;
}
var option1 = {
  title: {
    text: '每年浪费情况',
    left: '1%',
    top: 5,
    textStyle: {
      fontSize:13
    }, 
  },
  // tooltip: {
  //   trigger: 'axis',
  //   axisPointer: {
  //    type: 'cross'
  //   }
  // },
  grid:{
    top:50,
    bottom:30,
    left:30,
    right:10
  },
  xAxis: {
    type: 'category',
    data:["16", "17", "18","19","20"],
    // name:'时间/年',
    // nameGap: 3.5,
    min: 0
  },
  yAxis: {
      type: 'value',
      // name:'份数/份',
      // nameGap: 8,
      axisLine: {
        show: true
      }
  },
  series: [{
    data: [150,200,300,400,500],
    type: 'line'
  }]
};

function initChart1(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  chart.setOption(option1);
  return chart;
}
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图
    swiperImg:[
      {
        url:'cloud://yyf-1gde92y2b5d3dfd2.7979-yyf-1gde92y2b5d3dfd2-1304939590/shouye/swiper1.png',
        urlHover:''
      },
      {
        url:'cloud://yyf-1gde92y2b5d3dfd2.7979-yyf-1gde92y2b5d3dfd2-1304939590/shouye/swiper2.png',
        urlHover:''
      },
      {
        url:'cloud://yyf-1gde92y2b5d3dfd2.7979-yyf-1gde92y2b5d3dfd2-1304939590/shouye/swiper3.png',
        urlHover:''
      }
    ],
    // 粥
    zhouList:[
      {
        url:'../../../../images/zhou.png',
        title:'营养八宝粥',
        address:'田园食堂一层A1窗口',
        price:'1.5',
        surplus:'30'
      },
      {
        url:'../../../../images/zhou.png',
        title:'营养八宝粥',
        address:'田园食堂一层A1窗口',
        price:'1.5',
        surplus:'30'
      },
      {
        url:'../../../../images/zhou.png',
        title:'营养八宝粥',
        address:'田园食堂一层A1窗口',
        price:'1.5',
        surplus:'30'
      }
    ],
    // 节约之最
    jieyue:[],
    ec: {
      onInit: initChart
    },
    ec1: {
      onInit: initChart1
    },
    show: false,
    isshow:false,
    // 我的成就数据
    countOrder:10,
    student:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.cloud.callFunction({
      name : "gao",
      data:{
        action:"mealRankingList2",
        limit:3
      }
    }).then(res => {
      that.setData({
        zhouList : res.result.data
      })
    }).catch(err => {
    })

    wx.cloud.callFunction({
      name : "gao",
      data:{
        action:"canteenRankingList",
        limit: 4
      }
    }).then(res => {
      that.setData({
        jieyue : res.result.data
      })
    }).catch(err => {
    })

    wx.getStorage({
      key: "student",
      success (res) {
        that.setData({
          student:{
            id:res.data.student_id,
            name:res.data.student_name ,
            phone:res.data.student_phone 
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 订餐
  recommendbtn(){
    wx.reLaunch({
      url: '/pages/restaurant/restaurant'
    })
  }
  ,
  // 成就
  achievebtn(){
    var that = this
    that.loginafter()
  },
  loginafter: function(){
    let that = this
    if(this.data.student.id==null){
      Dialog.confirm({
        title: '提示',
        message: '未登录不能使用该功能',
        confirmButtonText:'完善信息'
      })
      .then(() => {
        wx.navigateTo({
          url: '/pages/prefer/user/user'
        })
      })
      .catch(() => {
        console.log("取消");
      });
    }

    

    if(this.data.student.id!=null){
     
      wx.cloud.callFunction({
        name : "gao",
        data:{
          action:"orderInfoCount",
          value:that.data.student.id
        }
      }).then(res => {
        that.setData({
          countOrder:res.result.list.length
        })
        that.setData({ isshow: true });
      }).catch(err => {
      })
    }   
  },
  // 倡议
  advocatebtn(){
    // console.log("111")
    this.setData({ show: true });
  },
  onClickHide(){
    this.setData({
       show: false,
       isshow: false
   });
  },
  // 预定
  yudingBtn:function(){
    wx.switchTab({
      url: '/pages/restaurant/restaurant'
    })
  }
})